/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.example.netcracker;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import ru.example.netcracker.model.DataBaseProvider;
import ru.example.netcracker.model.TaskProvider;
import ru.example.netcracker.model.XMLProvider;

/**
 *
 * @author Nikolay
 */
public class FileSettings {
    
    private static final Logger LOG = Logger.getLogger(FileSettings.class.getName());
    private static final FileSettings settings = new FileSettings();
    
    public static FileSettings getInstance() {
        return settings;
    }
    
    private final Properties properties;
    
    public FileSettings() {
        properties = new Properties();        
    }
    
    public void loadSettings(String path) {
        /*LOG.log(Level.INFO, "loadSettings resource name: {0}", name);
        URL resourceURL = Startup.class.getResource(name);
        LOG.log(Level.INFO, "loadSettings resource URL: {0}", resourceURL);
        URI resourceURI = null;
        try {
            resourceURI = resourceURL.toURI();
            LOG.log(Level.INFO, "loadSettings resource URI: {0}", resourceURI);
        } catch (URISyntaxException ex) {
            LOG.log(Level.SEVERE, "Exception ", ex);
        }
        if (resourceURI != null) {
            File resource = new File(resourceURI);
            try (InputStream input = new FileInputStream(resource);) {
                properties.load(input);
                LOG.log(Level.INFO, "loadSettings loaded settings: {0}", properties);
            } catch (IOException ex) {
                LOG.log(Level.SEVERE, "Exception ", ex);
            }
        }*/
        LOG.log(Level.INFO, "loadSettings resource file path: " + path);
        if(path != null) {
            try (InputStream input = new FileInputStream(path);) {
                properties.load(input);
                LOG.log(Level.DEBUG, "loadSettings loaded settings: " + properties);
            } catch (Exception ex) {
                LOG.log(Level.ERROR, "Exception ", ex);
            }
        } else {
            LOG.log(Level.ERROR, "path resource file == null");
        }
    }
    
    public TaskProvider getTaskProvider() {
        String prop = properties.getProperty("provider", "xml");
        LOG.log(Level.INFO, "TaskProvider: " + prop);
        if(prop.equalsIgnoreCase("db"))
            return new DataBaseProvider();
        return new XMLProvider();
    }
    
    public File getDefaultAlarm() {
        String prop = properties.getProperty("defaultalarm", null);
        LOG.log(Level.INFO, "default alarm path: " + prop);
        if(prop != null) {
            File alarm = new File(prop);
            if(alarm.exists()) {
                LOG.log(Level.DEBUG, "default alarm sound file: " + alarm.getAbsolutePath());
                return alarm;
            }
        }
        LOG.log(Level.ERROR, "No default alarm sound! Error path to the sound file in the properties file.");        
        return null;
    }
    
    public File getTaskFile() {
        String prop = properties.getProperty("tasksxml", "resources/tasks.xml");
        LOG.log(Level.INFO, "path file for save and load task in xml provider: " + prop);
        if(prop != null) {
            File alarm = new File(prop);
            LOG.log(Level.DEBUG, "xml task file: " + alarm.getAbsolutePath());
            return alarm;
        }
        LOG.log(Level.WARN, "No file for save and load task in xml provider!");        
        return null;
    }
}
